﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KTOP.Core;
using KTOP.Core.Domain.Matrix;
using KTOP.Core.Extensions;
using KTOP.Core.Formatters;
using KTOP.Core.Netlists;
using KTOP.Homeworks.Hw7.Logics;

namespace KTOP.Homeworks.Hw7
{
    public class Homework7 : IKtopHomework
    {
        public void Do(Netlist netlist)
        {
            var tw = Console.Out;
            
            var rMatrix = RMatrix.CreateFromNetlist(netlist);
            var mixed = new MatrixWithSumms(MatrixMixer.Mix(rMatrix));

            var matrixFormatter = new MatrixFormatter();
            var netlistFormatter = new NetlistFormatter();
            var textTableFormatter = new TextTableFormatter();
           
            netlistFormatter.Format(netlist, "source netlist", tw);
            tw.WriteLine();

            matrixFormatter.Format(rMatrix, "r matrix", tw);
            tw.WriteLine();

            matrixFormatter.Format(mixed, "remixed matrix", tw);
            tw.WriteLine();

            var sX = rMatrix.Matrix.Select(row => row.Sum()).ToArray();
            var sY = mixed.Matrix.Select(row => row.Sum()).ToArray();

            var sXTable = ExtEnumerable.One(Enumerable.Range(1, sX.Length))
                .Concat(ExtEnumerable.One(sX))
                .Select(t => t.Select(e => e.ToString()).ToArray())
                .ToArray();

            var sYTable = ExtEnumerable.One(Enumerable.Range(1, sY.Length))
                .Concat(ExtEnumerable.One(sY))
                .Select(t => t.Select(e => e.ToString()).ToArray())
                .ToArray();

            tw.WriteLine("S(X)");
            textTableFormatter.Format(tw, sXTable, TextTableFormatter.Borders.None);
            tw.WriteLine();

            tw.WriteLine("S(Y)");
            textTableFormatter.Format(tw, sYTable, TextTableFormatter.Borders.None);
            tw.WriteLine();
        }
    }
}
